package com.cskaoyan.service.admin.impl;

import com.cskaoyan.bean.pojo.Region;
import com.cskaoyan.bean.pojo.RegionExample;
import com.cskaoyan.mapper.RegionMapper;
import com.cskaoyan.service.admin.RegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Name : RegionServiceImpl.java
 * @Time : 2021/8/13 14:46
 * @Author : Xfeng
 * @Software : IntelliJ IDEA
 */
@Service
@Transactional
public class RegionServiceImpl implements RegionService {

    @Autowired
    RegionMapper regionMapper;

    @Override
    public List<Region> list() {
        RegionExample regionExample1 = new RegionExample();
        regionExample1.createCriteria().andTypeEqualTo(new Byte("1"));
        List<Region> provinces = regionMapper.selectByExample(regionExample1);
        for (Region province : provinces) {
            Integer provinceCode = province.getCode();//11
            RegionExample regionExample2 = new RegionExample();
            regionExample2.createCriteria().andTypeEqualTo(new Byte("2")).andCodeBetween((provinceCode * 100),(provinceCode * 100 + 100));//1100-1199
            List<Region> citys = regionMapper.selectByExample(regionExample2);
            for (Region city : citys) {
                Integer cityCode = city.getCode();//1101
                RegionExample regionExample3 = new RegionExample();
                regionExample3.createCriteria().andTypeEqualTo(new Byte("3")).andCodeBetween((cityCode * 100),(cityCode * 100 + 100));//110100-110199;
                List<Region> districts = regionMapper.selectByExample(regionExample3);
                city.setChildren(districts);
            }
            province.setChildren(citys);
        }
        return provinces;
    }
}
